#

| Chapter 16 Where Did I Put That? |

## 16.25 Listing Files by Age and Size

If you find a large directory, and most of the files are new, that directory may not be suitable for removal, as it is still being used. Here is a script that lists a summary of file sizes, broken down into the time of last modification. You may remember that ls -l will list the month, day, hour, and minute if the file is less than six months old, and show the month, day, and year if the file is more than six months old. Using this, the script creates a summary for each of the last six months, and each year for files older than that:

    ${*:-.} xargs
    #!/bin/sh
    # usage: age_files [directory ...]
    # lists size of files by age
    #
    # pick which version of ls you use
    #   System V
    #LS="ls -ls"
    #   Berkeley
    LS="ls -lsg"
    #
    find ${*:-.} -type f -print | xargs $LS | awk  '
    # argument 7 is the month; argument 9 is either hh:mm or yyyy
    # test if argument is hh:mm or yyyy format
    {
       if ($9 !~ /:/) {
          sz[$9]+=$1;
       } else {
          sz[$7]+=$1;
       }
    }
    END {
       for (i in sz) printf("%d\t%s\n", sz[i], i);
    }' | sort -nr

The program might generate results like this:

>     5715   1991
>     3434   1992
>     2929   1989
>     1738   Dec
>     1495   1990
>     1227   Jan
>     1119   Nov
>     953   Oct
>     61   Aug
>     40   Sep

- BB

16.24 Counting Files by Types |  | 16.26 Finding Text Files with findtext